﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace IntegratedOnBordDebug
{
    public class Common
    {
        // <summary>枚举本地 IPv4 地址
        /// 
        /// </summary>
        /// <returns></returns>
        public static string[] GetHostIPv4List()
        {
            List<string> list = new List<string>();
            IPAddress[] srcIps = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress item in srcIps)
            {
                if (item.AddressFamily == AddressFamily.InterNetwork)
                {
                    list.Add(item.ToString());
                }
            }
            return list.ToArray();
        }

        /// <summary>单字节转换成十六进制字符显示
        /// 
        /// </summary>
        /// <param name="Tbyte">目标字节</param>
        /// <param name="Formats">十六进制位数格式 X2 单字节，X4 两字节</param>
        /// <returns></returns>
        public static string OneByteToHex(byte Tbyte, string Formats = "X2")
        {
            return Tbyte.ToString(Formats);
        }

        /// <summary>字节数组转换成十六进制字符显示
        /// 
        /// </summary>
        /// <param name="ArrayBytes">目标字节数组</param>
        /// <returns></returns>
        public static string BytesToHex(byte[] ArrayBytes, string Split = " ")
        {
            string hexStr = "";
            for (int i = 0; i < ArrayBytes.Length; i++)
            {
                hexStr += hexStr.Equals("") ? ArrayBytes[i].ToString("X2") : Split + ArrayBytes[i].ToString("X2");
            }
            return hexStr;
        }

        //private static object lockObj = new object();
        public static bool Log(string Msg, bool IsDebug = false)
        {
            bool flag = false;
            try
            {
                if (!string.IsNullOrEmpty(Msg))
                {
                    DateTime cTime = DateTime.Now;
                    string basePath = Environment.CurrentDirectory + "\\log\\";
                    if (!Directory.Exists(basePath))
                    {
                        Directory.CreateDirectory(basePath);
                        //Thread.Sleep(100);
                    }
                    string rMsg;

                    basePath = IsDebug ? basePath + cTime.ToString("yyyyMMdd") + "_error.txt" : basePath + cTime.ToString("yyyyMMdd") + ".txt";
                    StreamWriter sw = null;
                    try
                    {
                        if (!File.Exists(basePath))
                        {
                            sw = File.CreateText(basePath);
                        }
                        else
                        {
                            sw = File.AppendText(basePath);
                        }

                        rMsg = string.Format("{0}  {1} ", cTime.ToString(), Msg);
                        sw.WriteLine(rMsg);
                        flag = true;
                    }
                    catch
                    {
                    }
                    finally
                    {
                        if (sw != null)
                        {
                            sw.Close();
                            sw.Dispose();
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                return flag;
            }
            return flag;
        }
    }
}
